<template>
  <div>
    <div v-loading="pageLoading">
      <!-- 个人信息 -->
      <modPersonalInfo ref="refModPersonalInfo" :check-mode="true" :dict="dict.dict" :user-data="userData" class="gl_mb_20" />

      <!-- 中跆协会员 -->
      <modMemberInfo v-if="hasModMemberInfo" ref="refModMemberInfo" :is-check="true" :dict="dict.dict" :user-data="userData" class="gl_mb_20" />

      <!-- 中跆协级位段位 -->
      <modTkwChiDan v-if="hasModTkwChiDan" ref="refModTkwChiDan" :is-check="true" :dict="dict" :user-data="userData" class="gl_mb_20" />

      <!-- 国际台协段位 -->
      <modTkwIntDan v-if="hasModTkwIntDan" ref="refModTkwIntDan" :is-check="true" :dict="dict" :user-data="userData" class="gl_mb_20" />

      <!-- 培训需要的信息 -->
      <modTrainInfo ref="refModTrainInfo" :is-check="true" :custom-setting-title="trainInfo.setting ? trainInfo.setting.customSettingTitle : ''" :register-train-info="registerTrainInfo" />
    </div>
  </div>
</template>

<script>
import { mapState, mapActions } from 'vuex';

import modPersonalInfo from '@/views/home/train/components/modPersonalInfo';
import modMemberInfo from '@/views/home/train/components/modMemberInfo';
import modTkwChiDan from '@/views/home/train/components/modTkwChiDan';
import modTkwIntDan from '@/views/home/train/components/modTkwIntDan';

import modTrainInfo from '@/views/home/train/components/modTrainInfo';

import {
  api_GET_EventsManagementRegisterId // 获取注册信息
} from '@/extend/api/gameswaggerApi/app';

import {
  api_GET_TrainTrainningId,
  api_GET_TrainTrainningRegisterBySourceRegister // 根据培训Id和会员Id获取培训人员详情
} from '@/extend/api/trainswaggerApi/app.js';

export default {
  components: {
    modPersonalInfo,
    modMemberInfo,
    modTkwChiDan,
    modTkwIntDan,
    modTrainInfo
  },
  data () {
    return {
      registerId: '',
      trainningId: '',
      pageLoading: false,
      userData: {}, // 用户基本数据
      trainInfo: {}, // 培训数据,
      registerTrainInfo: {}
    };
  },
  computed: {
    ...mapState({
      dict: state => state.dict
    }),
    // 是否需要中跆协会员信息
    hasModMemberInfo () {
      if (this.trainInfo.id) {
        return this.trainInfo.setting && this.trainInfo.setting.isCTAMemberRequired;
      } else {
        return false;
      }
    },
    // 是否需要中跆协级位段位
    hasModTkwChiDan () {
      if (this.trainInfo.id) {
        return this.trainInfo.setting && this.trainInfo.setting.isCTABeltRequired;
      } else {
        return false;
      }
    },
    // 是否需要国际台协段位
    hasModTkwIntDan () {
      if (this.trainInfo.id) {
        return this.trainInfo.setting && this.trainInfo.setting.isWTABeltRequired;
      } else {
        return false;
      }
    }
  },
  created () {
    const { query } = this.$route;
    this.registerId = query.registerId;
    this.trainningId = query.trainningId;
    this.getDict();
    this.getDictByType(['district']);
    if (query.registerId && query.trainningId) {
      // 获取培训信息
      this.getTrainInfoById();
      // 获取用户信息
      this.getUserInfo(query.registerId, {
        IncludeDelegation: true
      });
      // 获取培训特有信息
      this.getRegisterTrainInfo({
        trainningId: query.trainningId,
        registerId: query.registerId
      });
    }
  },
  methods: {
    ...mapActions({ getDict: 'dict/getDict', getDictByType: 'dict/getDictByType' }),
    // 根据赛事路由获取赛事信息
    getTrainInfoById() {
      const trainQO = {};
      trainQO.IncludeTrainningType = true;
      trainQO.IncludeTrainningStatus = true;
      trainQO.IncludeTrainningApproveStatus = true;

      trainQO.IncludeProvince = true;
      trainQO.IncludeCity = true;
      trainQO.IncludeCounty = true;
      trainQO.IncludeCommittee = true;
      trainQO.IncludeSetting = true;
      trainQO.IncludeSchedules = false;
      trainQO.IncludeHotels = false;
      trainQO.IncludeMembershipFees = false;
      trainQO.IncludeFees = false;

      api_GET_TrainTrainningId(this.trainningId, trainQO)
        .then(res => {
          this.trainInfo = res || {};
        })
        .catch(
          error => {
            this.$message({
              message: `获取培训信息失败 ${error.error.message}`,
              type: 'error',
              showClose: true
            });
          }
        );
    },
    // 获取用户信息
    getUserInfo (registerId, qo) {
      // 获取个人注册信息
      api_GET_EventsManagementRegisterId(registerId, qo)
        .then(res => {
          this.userData = res;
        })
        .catch(
          error => {
            this.$message({
              message: `获取用户信息失败 ${error.error.message}`,
              type: 'error',
              showClose: true
            });
          }
        );
    },
    // 获取培训用户特有信息
    getRegisterTrainInfo (qo) {
      api_GET_TrainTrainningRegisterBySourceRegister(qo)
        .then(
          res => {
            if (res.customFields && res.customFields.length) {
              res.customFields.sort((a, b) => a.sortOrder - b.sortOrder);
            }
            this.registerTrainInfo = res;
          }
        )
        .catch(
          error => {
            this.$message({
              message: `培训信息获取失败 ${error.error.message}`,
              type: 'error',
              showClose: true
            });
          }
        );
    }
  }
};
</script>

<style>
  .box_wrap {
    padding: 30px 20px;
    background-color: #fff;
    border-radius: 8px;
    box-shadow: 0px 5px 18px 0px rgba(0, 0, 0, 0.06);
  }
  .box_wrap_title {
    font-size: 16px;
    font-weight: 500;
    line-height: 22px;
    color: #353434;
    margin-bottom: 20px;
  }
</style>
